package com.lp.biz.members.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lp.biz.members.model.entity.BizMembers;
import com.lp.biz.members.mapper.BizMembersMapper;
import com.lp.biz.members.model.vo.Members;
import com.lp.biz.members.service.BizMembersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lp.common.exception.BaseException;
import com.lp.common.exception.ErrorCodeEnum;
import com.lp.common.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import static com.lp.common.constant.MembersConst.*;

/**
 * @program: backend
 * @description: BizMembers 服务实现类
 * @author Ke.Song
 * @since 2024-04-14 22:28:26
 */
@Service
public class BizMembersServiceImpl extends ServiceImpl<BizMembersMapper, BizMembers> implements BizMembersService {

    @Autowired
    private BizMembersMapper membersMapper;

    @Override
    public CommonResult getMembersList() {
        List<Members> result = new ArrayList<>();
        for (BizMembers bizMembers : membersMapper.selectList(new QueryWrapper<BizMembers>().lambda().orderByDesc(BizMembers::getType))) {
            Members members = new Members();
            members.setId(bizMembers.getId());
            members.setType(bizMembers.getType());
            members.setPrice(bizMembers.getActualPrice());
            result.add(members);
        }
        return CommonResult.success(result);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateMembers(List<Members> members) {
        // 检查输入列表是否为空
        if (members.isEmpty()) {
            throw new BaseException(ErrorCodeEnum.PARAMS_ERROR);
        }

        // 使用映射来去重并验证成员类型
        Map<Integer, Members> maps = new HashMap<>(6);
        for (Members temp : members) {
            Integer type = temp.getType();
            // 检查类型和价格是否为空
            if (type == null || temp.getPrice() == null) {
                throw new BaseException(ErrorCodeEnum.PARAMS_ERROR);
            }
            // 检查类型是否有效
            if (!type.equals(TYPE_MONTH) && !type.equals(TYPE_QUARTER) && !type.equals(TYPE_YEAR)) {
                throw new BaseException(ErrorCodeEnum.PARAMS_ERROR);
            }
            maps.put(temp.getType(), temp);
        }

        // 确保映射中的元素数量与输入列表相同，防止类型重复或遗漏
        if (maps.size() < members.size()) {
            throw new BaseException(ErrorCodeEnum.PARAMS_ERROR);
        }

        // 更新已存在成员的价格信息
        for (BizMembers bizMembers : membersMapper.selectList(new QueryWrapper<BizMembers>().lambda()
                .in(BizMembers::getType, maps.keySet()))) {
            Members remove = maps.remove(bizMembers.getType());
            // 确保数据库中的成员类型在输入列表中存在
            if (remove == null) {
                throw new BaseException(ErrorCodeEnum.PARAMS_ERROR);
            }
            // 如果价格不同，则更新价格信息
            if (!bizMembers.getActualPrice().equals(remove.getPrice())) {
                bizMembers.setOriginPrice(remove.getPrice());
                bizMembers.setActualPrice(remove.getPrice());
                membersMapper.updateById(bizMembers);
            }
        }

        // 插入新成员信息到数据库
        for (Members value : maps.values()) {
            BizMembers bizMembers = new BizMembers();
            bizMembers.setType(value.getType());
            bizMembers.setOriginPrice(value.getPrice());
            bizMembers.setActualPrice(value.getPrice());
            membersMapper.insert(bizMembers);
        }
    }

}
